package com.example.listener;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhuyaotong
 * @date 2021/5/17
 */
@Data
@Slf4j
@Service
public class TaskSequentialListenerComplete implements TaskListener {

    /*
        <activiti:taskListener event="complete" delegateExpression="${taskSequentialListenerComplete}" />
    */

    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Override
    public void notify(DelegateTask task) {
        /**
         * 不知道是不是activiti7的bug，我是遇到了，这个值不会动，详见
         * https://segmentfault.com/a/1190000019575655
         */
        Integer completeInstances1 = task.getExecution().getParent().getVariable("nrOfCompletedInstances", Integer.class);
        Integer nrOfActiveInstances1 = task.getExecution().getParent().getVariable("nrOfActiveInstances", Integer.class);
        task.getExecution().setVariable("completeInstances", completeInstances1);
        task.getExecution().setVariable("nrOfActiveInstances", nrOfActiveInstances1);
    }

}
